package com.bank.conrtoller;

import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bank.code.RespCode;
import com.bank.entity.Account;
import com.bank.excption.CommonException;
import com.bank.service.AccountService;
import com.bank.service.HandAccountService;
import com.bank.util.CommonUtil;

/*帐务交易主控制类*/
@Controller
public class HandAccountController extends BaseController{

		Logger logger = Logger.getLogger(HandAccountController.class);
		
		@Autowired
		private HandAccountService handAccountService;
		
		@Autowired
		private AccountService accountService;
		
		//存款
		@RequestMapping(value="doDeposit.do", method=RequestMethod.POST)
		public @ResponseBody Map<String, Object> doDeposit(@RequestBody Map<String,Object> params) throws CommonException{
			logger.debug("doDeposit.dododododod");
			String accountNo = (String)params.get("accont_number");
			String tellerId = (String) params.get("teller");
			String transOri = (String) params.get("trans_ori");
			String workdate =CommonUtil.getWorkDate();
			params.put("work_date", workdate);
			logger.debug("teller"+tellerId);
			logger.debug("transOri"+transOri);
			logger.debug("workdate"+workdate);

			String tellerSerno = CommonUtil.getTellerSerno(tellerId, transOri, workdate);
			params.put("teller_serno", tellerSerno);
			Account account = accountService.getAccount(accountNo);
			if(account == null){
				throw new CommonException(RespCode.ACCT_ISNOT_EXISTS, "账号不存在");
			}
			if(account.getAccount_status().equals("1")==false){	// 1-正常    0-销户   2-挂失
				throw new CommonException(RespCode.ACCT_STATUS_NOT_CORRECT, "账户状态不正确");
			}
					
			params.put("account", account);
			handAccountService.deposit(params);
			return this.setAndGetSuccess();
		}
		@RequestMapping(value="doWithrawal.do", method=RequestMethod.POST)
		public @ResponseBody Map<String, Object> doWithrawal(@RequestBody Map<String,Object> params) throws CommonException{
			
			String accountNo = (String)params.get("accont_number");
			String trans_password =(String)params.get("trans_password");
			String tellerId = (String) params.get("teller");
			String transOri = (String) params.get("trans_ori");
			String workdate =CommonUtil.getWorkDate();
			params.put("work_date", workdate);
			String tellerSerno = CommonUtil.getTellerSerno(tellerId, transOri, workdate);
			params.put("teller_serno", tellerSerno);
			Account account = accountService.getAccount(accountNo);
			if(account == null){
				throw new CommonException(RespCode.ACCT_ISNOT_EXISTS, "账号不存在");
			}
			if(account.getAccount_status().equals("1")==false){	// 1-正常    0-销户   2-挂失
				throw new CommonException(RespCode.ACCT_STATUS_NOT_CORRECT, "账户状态不正确");
			}
			if(account.getPassword().equals(trans_password)){	// 1-正常    0-销户   2-挂失
				throw new CommonException(RespCode.PASSWORD_NOT_CORRECT, "密码不正确");
			}
			
			params.put("account", account);
			handAccountService.withrawal(params);
			return this.setAndGetSuccess();
		}
		@RequestMapping(value="queryAccountDetailList.do", method=RequestMethod.POST)
		public @ResponseBody Map<String, Object>  queryAccountDetailList(@RequestBody Map<String,Object> params) throws CommonException{
			logger.debug("开始查询帐务明细");
			List<Object> value = handAccountService.getAccountDetailList(params);
			Integer total = handAccountService.getAccounDetailtListCount(params);
			this.setMapValue("total", total);
			this.setMapListValue("data", value);
			return this.returnMap();
			
		}
		
}
